var twbGoodsListVue = new Vue({
    el: '#rrapp',
    data: {
        twbGoodsListModel: {
            stepOne: true
        },
        page: {
            current: 1, // 当前页
            size: 10, // 分页数
            total: 0, // 总条数
        },
        q: {
            marketable: null
        },
        twbGoodsList: [],
        dialogs: {
            editPriceDialog: false,
        },
        twbEditPriceVoList: [],
        columnNames: [],
    },
    mounted: function () {
        this.twbGoodsListSearch();

    },
    watch:{
        //q.marketable
        'q.marketable':function(val, oldVal) {
            this.twbGoodsListSearch(true);
        }
    },
    methods: {
        loadData:function(parms){
            if(parms.refresh){
                this.twbGoodsListSearch();
            }
        },
        pushGoodsClick: function () {
            //todo
            this.twbGoodsListModel.stepOne = true;
        },

        detailGoods: function (goodsId,twbPushFlag) {
            if(!twbPushFlag){
                alert("旧商品请直接修改价格等参数,复杂编辑请重新发布一个新商品!")
                return;
            }
            this.twbGoodsListModel.stepOne = false;
            this.$refs.zyPushGoods.initData(goodsId);
        },

        editPrice: function (goodsId) {
            var self = this;
            $.ajax({
                type: "POST",
                url: baseURL + "goods/goods/editTwbGoodsPrice/" + goodsId,
                contentType: "application/json",
                success: function (r) {
                    if (r.code == 0) {
                        self.twbEditPriceVoList = r.twbEditPriceVoList;
                        self.columnNames = [];
                        for (var i = 0; i < self.twbEditPriceVoList[0].goodsOptionEntityList.length; i++) {
                            var optionEntity = self.twbEditPriceVoList[0].goodsOptionEntityList[i];
                            self.columnNames.push(optionEntity.attributeName);
                        }
                    } else {
                        alert(r.msg);
                    }
                }

            })
            this.dialogs.editPriceDialog = true;
        },
        //把每一行的索引放进row-->配合点击事件获取索引
        tableRowClassName({row, rowIndex}) {
            row.index = rowIndex;
            if (rowIndex === this.entityIndex) {
                return 'success-row';
            }
            return '';
        },

        handleSizeChange: function (val) {
            this.page.size = val;
            this.twbGoodsListSearch();
        },

        handleCurrentChange: function (val) {
            this.page.current = val;
            this.twbGoodsListSearch();
        },
        //图文宝列表分页查询
        twbGoodsListSearch: function (mark) {
            var paramData = {};
            paramData.marketable = this.q.marketable;
            paramData.current = this.page.current;
            paramData.size = this.page.size;
            paramData.total = this.page.total;
            if(mark){
                this.page.current = 1;
                paramData.current = 1;
            }
            var self = this;
            $.ajax({
                type: "POST",
                url: baseURL + "goods/goods/getTwbGoodsList",
                contentType: "application/json",
                data: JSON.stringify(paramData),
                success: function (r) {
                    if (r.code == 0) {
                        self.page.current = r.goodsEntityPage.current;
                        self.page.size = r.goodsEntityPage.size;
                        self.page.total = r.goodsEntityPage.total;
                        self.twbGoodsList = r.goodsEntityPage.records;
                    } else {
                        alert(r.msg);
                    }
                }
            })
        },
        //详细页面保存价格
        saveEditPrice: function () {
            var self = this;
            $.ajax({
                type: "POST",
                url: baseURL + "goods/goods/saveTwbEditPrice",
                contentType: "application/json",
                data: JSON.stringify(self.twbEditPriceVoList),
                success: function (r) {
                    if (r.code == 0) {
                        //改价成功,刷新页面
                        self.resetEditPrice();
                        self.twbGoodsListSearch();
                    } else {
                        alert(r.msg);
                    }
                }
            })
        },
        //保存成功调用重置元素
        resetEditPrice: function () {
            this.twbEditPriceVoList = [];
            this.columnNames = [];
            this.dialogs.editPriceDialog = false;
        },
        //列表批量操作商品状态
        listOperation:function(marketable){
            var self = this;
            var selection = this.$refs.twbGoodsListRef.selection;
            var ids = [];
            for (var selectionElement of selection) {
                ids.push(selectionElement.goodsId);
            }
            console.info(selection)
            var str = null;
            if(marketable == 'delete'){
                str = "删除";
            }else if(marketable == 'puton'){
                str = "上架";
            }else{
                str = "下架";
            }
            this.$confirm('您确定要批量'+ str +'这些商品吗', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "goods/goods/twbListOperation/"+marketable,
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert("操作成功")
                            //改价成功,刷新页面
                            self.resetEditPrice();
                            self.twbGoodsListSearch();
                        } else {
                            alert(r.msg);
                        }
                    }
                });
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(function(){
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //table列名前加星号
        renderHeader:function(h, { column, $index }){
            return h(
                'span',
                [
                    h('span', {style:'color:red;margin-left:5px;'},'* '),
                    h('span', column.label),
                ],
            );
        },
        //跳转紫云网
        jumpZiyun:function(goodsId){
            window.open("//www.11ziyun.com/goods/"+goodsId);
        }
    }
});